package com.zszedu.service;

import com.zszedu.mapper.AccountMapper;
import com.zszedu.mapper.IncomeMapper;
import com.zszedu.pojo.Account;
import com.zszedu.pojo.Income;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@DubboService
public class IncomeServiceImpl implements IncomeService{
    @Autowired
    private IncomeMapper incomeMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public List<Income> queryByUid(Long uid) {
        return incomeMapper.queryByUid(uid);
    }

    @Override
    public void addIncome(Income income) {
        incomeMapper.addIncome(income);
    }

    @Override
    public List<Income> queryStatusAndDate(Date date) {
        return incomeMapper.queryStatusAndDate(date);
    }

    //每天12.30分进行扫描，查询收益表中状态为0收益时间小于当前日期的收益记录进行返回给用户资金表
    @Transactional
    @Scheduled(cron = "0 30 12 * * ?")
    public void income(){
        //查询收益表中状态为0，收益时间小于当前日期的收益记录
        List<Income> incomes = queryStatusAndDate(new Date());

        //获取的对象为空则不存在需要返回给用户资金表的用户
        if (incomes == null){
            return;
        }

        //执行到这里说明存在数据
        for (Income income : incomes) {
            Double money = income.getIncomeMoney() + income.getBidMoney();//获取收益金额以及投资金额
            accountMapper.editAddMoney(income.getUid(),money); //添加金额
            incomeMapper.editStatus(income.getId());//修改收益状态为 1 (已返)
        }
    }
}
